<?php 

namespace Home\Controller;
use Hdphp\Library\Controller;

/**
*  会员
*/
class MemberController extends CommonController
{
	//用户uid信息
	private $uid;

	public function __construct(){
		parent::__construct();
		//判断非法用户
		if( !isset($_GET['uid']) || intval($_GET['uid'])==0 ){
			$this->error('用户不存在','./index.php');
		}else{
			$uid=intval($_GET['uid']);
			$sql="SELECT * FROM hd_user WHERE uid ={$uid}";
			if(!M()->query($sql)){
				$this->error('用户不存在','./index.php');
			}
		}
		$this->uid=isset($_GET['uid']) ? (int)$_GET['uid'] : $_SESSION['uid'];
	}

	//会员中心首页
	public function index(){

		//导航分类
		$this->topNav();
		//用户uid
		$this->_myInfo($this->uid);
		//我的提问

		$sql="SELECT * FROM hd_ask as a JOIN hd_category as c ON a.cid=c.cid WHERE uid={$this->uid}";
		$myAsk=M()->query($sql);
		$this->assign('myAsk',$myAsk);
		$totalMyAsk=count($myAsk);
		$this->assign('totalMyAsk',$totalMyAsk);
// p($myAsk);die;

		//我的回答
		$sql="SELECT an.content as ancontent,a.content as acontent,an.uid as anuid,a.uid as auid,an.time as antime,a.time as atime,a.cid as acid,c.cid as ccid,an.*,a.*,c.* FROM hd_answer as an JOIN hd_ask as a ON an.asid=a.asid JOIN hd_category as c ON a.cid=c.cid WHERE an.uid={$this->uid}";
		$myAnswer=M()->query($sql);
		$this->assign('myAnswer',$myAnswer);
		$totalMyAnswer=count($myAnswer);
		$this->assign('totalMyAnswer',$totalMyAnswer);
// p($myAnswer);die;

		$this->display('member.html');
	}

	//我的提问
	public function my_ask(){
		//导航分类
		$this->topNav();

		//个人信息
		$this->_myInfo($this->uid);

		//我的提问 等待解决问题0和已经解决问题1
		$solve=isset($_GET['w']) ? intval($_GET['w']) : 0;
		$sql="SELECT * FROM hd_ask as a JOIN hd_category as c ON a.cid=c.cid WHERE uid={$this->uid} and solve={$solve}";

		$myAsk=M()->query($sql);
		$this->assign('myAsk',$myAsk);
		$totalMyAsk=count($myAsk);
		$this->assign('totalMyAsk',$totalMyAsk);
// p($myAsk);die;

		$this->display();
	}

	//我的回答
	public function my_answer(){

		//导航分类
		$this->topNav();		
		//个人信息
		$this->_myInfo($this->uid);

		//我的回答
		$accept=isset($_GET['w']) ? intval($_GET['w']) : 0;
		$sql="SELECT an.content as ancontent,a.content as acontent,an.uid as anuid,a.uid as auid,an.time as antime,a.time as atime,a.cid as acid,c.cid as ccid,an.*,a.*,c.* FROM hd_answer as an JOIN hd_ask as a ON an.asid=a.asid JOIN hd_category as c ON a.cid=c.cid WHERE an.uid={$this->uid} AND accept={$accept}";
		$myAnswer=M()->query($sql);
		$this->assign('myAnswer',$myAnswer);
		$totalMyAnswer=count($myAnswer);
		$this->assign('totalMyAnswer',$totalMyAnswer);
// p($myAnswer);die;
		$this->display();
	}

	//我的等级
	public function my_level(){

		//导航分类
		$this->topNav();		
		//个人信息
		$this->_myInfo($this->uid);

		$this->display();
	}

	//我的金币
	public function my_gold(){

		//导航分类
		$this->topNav();		
		//个人信息
		$this->_myInfo($this->uid);


		$this->display();
	}

	//上传图像
	public function my_face(){

		if(IS_POST){

			$upload=new \Hdphp\Tool\Upload;
			$result=$upload->up();

			if( !$result ){
				// echo $upload->error;
				$this->error($upload->error);
			}else{
				$thumb=new \Hdphp\Tool\Thumb;
				//返回新的文件名
				$result=$thumb->thumb(200,200,$result[0]);

				$sql="UPDATE hd_user SET face='{$result}' WHERE uid={$_SESSION['uid']}";

				if( M()->exec($sql) ){
					$this->success('上传成功',"./index.php?c=member&a=my_face&uid={$_SESSION['uid']}");
				}else{
					$this->error('上传失败');
				}
			}

		}

		//导航分类
		$this->topNav();		
		//个人信息
		$this->_myInfo($this->uid);


		$this->display();
	}


	private function _myInfo($uid){

		$sql="SELECT * FROM hd_user WHERE uid={$uid}";
		$userinfo=M()->query($sql);
		$this->assign('userinfo',$userinfo[0]);
		// p($userinfo);die;
	}

}

 ?>